package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.Episode;
import com.le123.ysdq.R;

import java.util.ArrayList;

/**
 * @author liu
 * 
 */
public class EpisodePlayerListAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<Episode> episodes;
	private LayoutInflater inflater;

	private String mSelectProder;

	private class ItemHolder {
		ImageView playTagIv;
		TextView episodePorderTv;
		TextView episodePorderNameTv;
	}

	public EpisodePlayerListAdapter(Context mContext, ArrayList<Episode> episodes) {
		super();
		this.mContext = mContext;
		this.episodes = episodes;
		this.inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return episodes.size();
	}

	@Override
	public Object getItem(int position) {
		if (null != episodes && position <= (episodes.size() - 1)) {
			return episodes.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ItemHolder holder = new ItemHolder();
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.episode_player_list_item, null);
			holder.playTagIv = (ImageView) convertView.findViewById(R.id.play_tag_iv);
			holder.episodePorderTv = (TextView) convertView.findViewById(R.id.episode_porder_tv);
			holder.episodePorderNameTv = (TextView) convertView.findViewById(R.id.episode_porder_name_tv);
			convertView.setTag(holder);
		} else {
			holder = (ItemHolder) convertView.getTag();
		}
		holder.episodePorderTv.setText(episodes.get(position).getPorder());
		holder.episodePorderNameTv.setText(episodes.get(position).getSubName());
		holder.playTagIv.setVisibility(View.INVISIBLE);
		setSelectTextColor(holder, position);

		return convertView;
	}

	private void setSelectTextColor(ItemHolder holder, int position) {
		Episode episode = episodes.get(position);
		if (mSelectProder.equals(episode.getPorder())) {
			holder.episodePorderTv.setSelected(true);
			holder.episodePorderNameTv.setSelected(true);
			holder.playTagIv.setVisibility(View.VISIBLE);
		} else {
			holder.episodePorderTv.setSelected(false);
			holder.episodePorderNameTv.setSelected(false);
			holder.playTagIv.setVisibility(View.INVISIBLE);
		}
	}

	public void setEpisodes(ArrayList<Episode> episodes) {
		this.episodes=episodes;
		notifyDataSetChanged();
	}

	public String getmSelectProder() {
		return mSelectProder;
	}

	public void setmSelectProder(String mSelectProder) {
		this.mSelectProder = mSelectProder;
	}
}
